<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:include="common/common_head :: commonHeader('新增组织机构')"/>
</head>

<body class="gray-bg">

<div class="wrapper wrapper-content animated fadeInRight">
    <form class="form-horizontal" method="post" th:action="@{/sys/dept/save}" id="deptForm">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>门店信息</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">门店形象：</label>
                            <div class="col-sm-6">
                                <div class="img-preview img-preview-sm image-preview-out">
                                    <input id="imageId" name="imageId" type="hidden" class="form-control">
                                    <img id="imageIdImg" class="image-preview" th:src="@{/static/img/add-pic.png}"
                                         onclick="openImgSelector()">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>机构名称：</label>
                            <div class="col-sm-6">
                                <input name="name" type="text" placeholder="机构名称" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>机构电话：</label>
                            <div class="col-sm-6">
                                <input name="mobile" type="text" placeholder="机构电话" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>上级机构：</label>
                            <div class="col-sm-6">
                                <input type="hidden" name="parentId" id="parentId" class="form-control"/>
                                <input onclick="addPid()" readonly="readonly" type="text" name="parentName"
                                       id="parentName" placeholder="请选择" class="form-control"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>排序码：</label>
                            <div class="col-sm-6">
                                <input name="orderNum" type="text" placeholder="排序码" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>机构地址：</label>
                            <div class="col-sm-2">
                                <select class="form-control m-b" id="provinceCode" name="provinceCode">
                                    <option value=''>请选择省份</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b" id="cityCode" name="cityCode">
                                    <option value=''>请选择城市</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b" id="areaCode" name="areaCode">
                                    <option value=''>请选择区县</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-3 col-sm-6">
                                <input id="address" name="address" type="text" placeholder="详细地址" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>经纬度：</label>
                            <div class="col-sm-2">
                                <input id="longitude" name="longitude" readonly placeholder="经度" class="form-control">
                            </div>
                            <div class="col-sm-2">
                                <input id="latitude" name="latitude" readonly placeholder="纬度" class="form-control">
                            </div>
                            <div class="col-sm-2">
                                <button onclick="showMap()" class="btn btn-default " type="button"><i class="fa fa-map-marker"></i>&nbsp;&nbsp;百度地图
                                </button>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="required-sign">*</span>成立时间：</label>
                            <div class="col-sm-6">
                                <input id="foundingTime" name="foundingTime" type="text" placeholder="成立时间" class="form-control laydate-icon">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-3 col-sm-6">
                                <button class="btn btn-primary" type="button" onclick="saveDept()">保存</button>
                                <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>
<!--baidu地图-->
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=8dv6wswj0tMtYSUO0PClskkTW5DsOEGi"></script>
<script th:src="@{/static/js/custom/baidumap.js}"></script>

<script type="text/javascript">
    $(document).ready(function () {
        // 初始化省市区
        Page.ajaxPOST(ctxPath + "/sys/address/getAllProvince", null, function (res) {
            $("#provinceCode").text("");
            var html = "<option value=''>请选择省份</option>";
            var provinceList = res.provinceList;
            for (var i = 0; i < provinceList.length; i++) {
                html += "<option value='" + provinceList[i].provinceCode + "'>" + provinceList[i].province + "</option>";
            }
            $("#provinceCode").append(html);
        })
        $("#provinceCode").change(function () {
            var provinceCode = $("#provinceCode").val();
            Page.ajaxPOST(ctxPath + "/sys/address/getCityListByProvinceCode/" + provinceCode, null, function (res) {
                $("#cityCode").text("");
                var html = "<option value=''>请选择城市</option>";
                var cityList = res.cityList;
                for (var i = 0; i < cityList.length; i++) {
                    html += "<option value='" + cityList[i].cityCode + "'>" + cityList[i].city + "</option>";
                }
                $("#cityCode").append(html);

                $("#areaCode").text("");
                $("#areaCode").append("<option value=''>请选择区县</option>");
            })
        })

        $("#cityCode").change(function () {
            var city = $("#cityCode").val();
            Page.ajaxPOST(ctxPath + "/sys/address/getAreaListByCityCode/" + city, null, function (res) {
                $("#areaCode").text("");
                var html = "<option value=''>请选择区县</option>";
                var areaList = res.areaList;
                for (var i = 0; i < areaList.length; i++) {
                    html += "<option value='" + areaList[i].areaCode + "'>" + areaList[i].area + "</option>";
                }
                $("#areaCode").append(html);
            })
        })
    });

    laydate.render({
        elem: '#foundingTime', //指定元素
        format: 'yyyy-MM-dd'
    });

    Page.validateForm("#deptForm",{
        tips:"确定保存？",
        rules: {
            name: {
                required: true
            },
            mobile:{
                required: true
            },
            foundingTime:{
                required: true
            },
            address:{
                required: true
            },
            provinceCode:{
                required: true
            },
            cityCode:{
                required: true
            },
            areaCode:{
                required: true
            },
            longitude:{
                required: true
            },
            latitude:{
                required: true
            }
        },
        messages: {
            name: {
                required: "请输入门店名称"
            },
            mobile:{
                required: '请输入门店电话'
            },
            foundingTime:{
                required: '请输入成立时间'
            },
            address:{
                required: '请输入地址'
            },
            provinceCode:{
                required: '请输入省份'
            },
            cityCode:{
                required: '请输入城市'
            },
            areaCode:{
                required: '请输入区县'
            },
            longitude:{
                required: '请以鼠标左键点击地图生成经纬度'
            },
            latitude:{
                required: '请以鼠标左键点击地图生成经纬度'
            }

        },
        submitSuccess:function(data){
            Dialog.msg("新建成功!",function(){
                Dialog.closeFrame(window.name)
            });
        }
    });

    function saveDept() {

        if($("#deptForm").valid()) {
            Page.ajaxPOST(ctxPath+"/sys/dept/save", $("#deptForm").serialize(), function (res) {
                Dialog.msg("操作成功", function () {
                    parent.searchDept();
                    Dialog.closeFrame(window.name);
                })
            })
        }
    }

    //弹出资源树
    function addPid() {
        Dialog.openUrl("组织机构列表", ctxPath+"/modules/sys/dept/dept_select.html", {area: ['300px', '400px'],shadeClose:
            true});
    }

    function toDeptId(treeNode) {
        $("#parentId").val(treeNode.deptId);
        $("#parentName").val(treeNode.name);
    }

    function openImgSelector() {
        var index = Dialog.openUrl("图片选择", ctxPath+"/sys/user/openImagePop?picId="+$("#imageId").val(),
            {area: ['900px', '700px'],shadeClose:
                true});
    }

    function setImageUrl(dataUrl) {
        Page.ajaxPOST(ctxPath+"/sys/user/uploadAvatar", {'base64Data': dataUrl}, function (res) {
            $("#imageIdImg").attr("src", ctxPath+'/sys/user/getPic/'+res.result);
            $("#imageId").val(res.result);
        })
    }

    /**
     * 弹出地图
     */
    function showMap() {
        var index = Dialog.openUrl("百度地图", ctxPath + '/common/baidu_map.html',
            {area: ['900px', '500px'], maxmin: true});
    }

    /**
     * 回显地图定位信息
     * @param address
     * @param lng
     * @param lat
     */
    function locationMap(address, lng, lat) {
        $("#longitude").val(lng);
        $("#latitude").val(lat);
        $("#address").val(address);
    }
</script>
</body>
</html>
